package com.situ.mall.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.situ.mall.common.entity.Member;
import com.situ.mall.common.exception.BusinessException;
import com.situ.mall.common.service.IMemberService;
import com.situ.mall.common.util.RequestUtil;
import com.situ.mall.common.util.SendSmsUtil;
import com.situ.mall.common.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("/")
public class LoginController {

    @Autowired
    private IMemberService memberService;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @GetMapping("/login")
    public String login() {
        return "login";
    }

    @PostMapping("/login")
    public String doLogin(String username, String password, Model model) {
        Object o = redisTemplate.opsForValue().get("err-" + username);
        if (o != null) {
            int i = (int) o;
            if (i == 5) {
                //给前台返回一个失败原因
                model.addAttribute("errMsg", "连续失败5次，锁定2小时，2小时后再试");
                return "login";
            }
        }
        boolean b = memberService.check(username, password);
        if (b) {
            //登录成功把之前保存的失败次数删除
            redisTemplate.delete("err-" + username);
            return "redirect:/";
        } else {//错了
            //在redis中记录　　　username　这个用户失败了
            //先判断之前有没有存过
            if (o != null) {
                redisTemplate.opsForValue().set("err-" + username, (int) o + 1, 1, TimeUnit.MINUTES);
            } else {
                redisTemplate.opsForValue().set("err-" + username, 1, 1, TimeUnit.MINUTES);
            }
            model.addAttribute("errMsg", "用户名密码错误,还有2次机会");
            return "login";
        }
    }

    @GetMapping("/logout")
    public String logout() {
        RequestUtil.logout();
        return "redirect:/login";
    }

    //接收一个手机号，给这个号发短信
    @PostMapping("/sendCode")
    @ResponseBody
    public String sendCode(String tel) {
        //验证之前这个手机号有没有注册过
        Member m = memberService.getOne(new LambdaQueryWrapper<Member>().eq(Member::getUsername, tel));
        if (m != null) {//说明之前注册过
            throw new BusinessException("手机号已被使用！");
        }
        //使用发短信的工具类发送短信
        int i = (int) (Math.random() * 900000 + 100000);
        redisTemplate.opsForValue().set("r-" + tel, i, 5, TimeUnit.MINUTES);
        //参数　　模版号，　手机号，内容
        SendSmsUtil.sendMessage("SMS_275345199", tel, String.valueOf(i));
        return String.valueOf(i);
    }

    @PostMapping("/register")
    public String register(String tel, String password, String code) {
        //先判断code对不对
        Object o = redisTemplate.opsForValue().get("r-" + tel);
        if (o == null || !o.equals(code)) {
            throw new BusinessException("验证码不正确");
        }
        //派memberService去注册这个账号
        Member m = new Member();
        m.setUsername(tel);
        m.setTel(tel);
        m.setPassword(StringUtil.md5(password));
        memberService.save(m);
        //返回登录页面
        return "login";
    }
}
